﻿using System;
using WinSource.Model.Helpers;

namespace WinSource.Model
{
    public class Article
    {
        #region Constructor

        public Article()
        {
        }

        public Article(string id, string thumbnail)
        {
            Id = id;
            Thumbnail = GetThumbnailFormat(thumbnail);
        }

        public Article(string id, string title, string description, DateTime publishDate, string author, string thumbnail)
        {
            Id = id;
            Title = title;
            Description = RemoveYouTubeVideos(description);
            PublishDate = publishDate;
            Author = author;
            Thumbnail = thumbnail;
        }

        #endregion

        #region Properties

        public string Id { get; set; }
        public string Title { get; set; }
        public DateTime PublishDate { get; set; }
        public string Author { get; set; }
        public string Thumbnail { get; set; }
        public string Description { get; set; }

        public string Url
        {
            get { return string.Format("http://winsource.com/?p={0}", Id); }
        }

        public string DateDifference
        {
            get
            {
                return DateHelper.GetDateDifferenceText(PublishDate);
            }
        }

        public DateTime PublishDateDay
        {
            get
            {
                return new DateTime(PublishDate.Year, PublishDate.Month, PublishDate.Day);
            }
        }
            
        #endregion

        #region Private Methods

        private string RemoveYouTubeVideos(string description)
        {
            string result = description;

            int positionYouTube = description.IndexOf("<iframe src=\"http://www.youtube.com");

            if (positionYouTube != -1)
            {
                int positionEndYouTube = description.IndexOf("</iframe>", positionYouTube);

                result = result.Remove(positionYouTube, positionEndYouTube - positionYouTube + 9);
            }

            return result;
        }

        private string GetThumbnailFormat(string image)
        {
            int positionPoint = image.LastIndexOf('.');
            string extension = image.Substring(positionPoint, 4);
            string imageWithoutExtension = image.Remove(positionPoint, 4);

            return string.Format("{0}-150x150{1}", imageWithoutExtension, extension);
        }

        #endregion
    }
}
